﻿using System;
using System.ServiceModel;

using SBPweb.RIA.Client.Controls.Base;

namespace SBPweb.RIA.Client.Controls.Extensions
{

    public static class ClientBaseExtenders
    {

        public static void FixEndpointAddress<T>(this System.ServiceModel.ClientBase<T> extendee, string lastFolderFragment)
            where T : class
        {
            Uri changedUri = extendee.Endpoint.Address.Uri;
            if (changedUri.Scheme == Uri.UriSchemeFile)
            {
                return;
            }
            string path = changedUri.LocalPath.ToLower();
            path = path.Substring(path.LastIndexOf(lastFolderFragment));
            changedUri = new Uri(ExtendableApplication.Current.HostBaseUri, path);
            extendee.Endpoint.Address = new System.ServiceModel.EndpointAddress(changedUri);
        }

    }

}